package com.wing.wecom.service;

import chat.qiye.wechat.sdk.api.contact.ContactUserApi;
import chat.qiye.wechat.sdk.api.contact.param.GetUserIdParam;
import chat.qiye.wechat.sdk.api.contact.resp.GetUserIdResp;
import chat.qiye.wechat.sdk.api.message.AppChatMessageApi;
import chat.qiye.wechat.sdk.api.message.dto.app.AppMessageTextDto;
import chat.qiye.wechat.sdk.api.message.resp.AppMessageSendResp;
import chat.qiye.wechat.sdk.common.dto.TextDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 企业微信消息推送
 *
 * @author
 */
@Service
@Slf4j
public class MessageServiceImpl implements MessageService {

    @Resource
    AppChatMessageApi appChatMessageApi;
    @Resource
    ContactUserApi contactUserApi;

    @Override
    public AppMessageSendResp sentMessage(String mobile, Integer agentid) {
        GetUserIdParam getUserIdParam = new GetUserIdParam();
        AppMessageTextDto appMessageDto = new AppMessageTextDto();
        getUserIdParam.setMobile(mobile);
        GetUserIdResp getUserIdResp = contactUserApi.getUserIdByPhone(getUserIdParam);
        if(getUserIdResp.getErrcode() == 0){
            appMessageDto.setTouser(getUserIdResp.getUserid());
            appMessageDto.setMsgtype("text");
            appMessageDto.setAgentid(agentid);
            TextDto text = new TextDto();
            text.setContent("领取刀具(测试刀具001)10支");
            appMessageDto.setText(text);
        }else{
            log.info("当前登录用户手机号没有企业微信匹配");
        }
        AppMessageSendResp appMessageSendResp = appChatMessageApi.sentMessage(appMessageDto);
        return appMessageSendResp;
    }
}
